package com.fs.erp.api.basedata.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.fs.erp.api.basedata.entity.*;
import com.fs.erp.api.basedata.mappers.ProductMapper;
import com.fs.erp.api.basedata.service.*;
import com.fs.erp.api.basedata.vo.QueryProductVo;
import com.fs.erp.api.basedata.vo.product.UpdateProductVo;
import com.fs.erp.common.utils.Assert;
import com.fs.erp.common.utils.BeanUtil;
import com.fs.erp.mybatis.resp.PageResult;
import com.fs.erp.mybatis.utils.PageHelperUtil;
import com.fs.erp.mybatis.utils.PageResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 商品管理
 */
@Service
public class ProductserviceImpl extends BaseMpServiceImpl<ProductMapper, Product>
    implements ProductService{
//    @Qualifier("productPurchaseService")
    @Autowired
    ProductPurchaseService productPurchaseService;

    @Autowired
    ProductRetailService productRetailService;

    @Autowired
    ProductSaleService productSaleService;

    @Autowired
    ProductSalepropItemRelationService productSalepropItemRelationService;
//    @Override
//    public PageResult<Product> query(Integer pageIndex, Integer pageSize, QueryProductVo vo) {
//        Assert.greaterThanZero(pageIndex);
//        Assert.greaterThanZero(pageSize);
//
//        PageHelperUtil.startPage(pageIndex,pageSize);
//
//        List<Product> products=getBaseMapper().query(vo);
//
//        return PageResultUtil.convert(new PageInfo<>(products));
//    }

    /**
     * 修改
     * @param vo
     */
    @Transactional
    @Override
    public void updateProduct(UpdateProductVo vo) {
        Product product=new Product();
        BeanUtil.copyProperties(vo,product,true);
        getBaseMapper().updateById(product);
        //判断销售价是否需要改变
        //采购价
        if (Objects.nonNull(vo.getPurchasePrice())){
            ProductPurchase productPurchase=new ProductPurchase();
            productPurchase.setId(product.getId());
            productPurchase.setPrice(vo.getPurchasePrice());
            productPurchaseService.updateByProductId(productPurchase);
        }
        //零售价
        if (Objects.nonNull(vo.getRetailPrice())){
            ProductRetail productRetail=new ProductRetail();
            productRetail.setId(product.getId());
            productRetail.setPrice(vo.getRetailPrice());
            productRetailService.updateByProductId(productRetail);
        }
        //销售价
        if (Objects.nonNull(vo.getSalePrice())){
            ProductSale productSale=new ProductSale();
            productSale.setId(product.getId());
            productSale.setPrice(vo.getSalePrice());
            productSaleService.updateByProductId(productSale);
        }
        ProductSalepropItemRelation salepropItemRelation=new ProductSalepropItemRelation();
        //销售属性
        salepropItemRelation.setSalePropItemId1(vo.getSalePropItem1Id());
        salepropItemRelation.setSalePropItemId2(vo.getSalePropItem2Id());
        productSalepropItemRelationService.updateByProductId(salepropItemRelation);
    }

    @Override
    public <S> S getThis(Class<S> clazz) {
        return null;
    }

    @Override
    public void cleanCacheByKey(Serializable key) {

    }

    @Override
    public void cleanCacheByKeys(Collection<? extends Serializable> keys) {

    }
}
